<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入法人身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所在城市">
        <provice-select
          :defaultCity="defaultCity1"
          @changeValue="queryChangeValue"
          :isShowDistrict="true"
        ></provice-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wb:wbMaintainCompany:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wb:wbMaintainCompany:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wb:wbMaintainCompany:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="wbMaintainCompanyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="公司名称"
        align="center"
        prop="companyName"
        width="180"
      >
        <template slot-scope="{ row }">
          <div @dblclick="handleDoubleClick(row)">{{ row.companyName }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="公司简介" align="center" prop="intro" show-overflow-tooltip width="200" /> -->
      <el-table-column label="联系人/法人" align="center" prop="contact" />
      <!-- <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="身份证正面" align="center" prop="idCardPicA" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.idCardPicA" :width="50" :height="50" />
        </template>
</el-table-column>
<el-table-column label="身份证反面" align="center" prop="idCardPicB" width="100">
  <template slot-scope="scope">
          <image-preview :src="scope.row.idCardPicB" :width="50" :height="50" />
        </template>
</el-table-column>
<el-table-column label="营业执照" align="center" prop="businessLicense" width="100">
  <template slot-scope="scope">
          <image-preview :src="scope.row.businessLicense" :width="50" :height="50" />
        </template>
</el-table-column> -->
      <el-table-column label="联系方式" align="center" prop="phoneNumber" />
      <el-table-column label="法人身份证号" align="center" prop="idCard" />
      <el-table-column label="城市" align="center">
        <template slot-scope="{ row }">
          {{ fomatterAdressName(row.province) }}/{{
            fomatterAdressName(row.city)
          }}/{{ fomatterAdressName(row.county) }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="详细地址" align="center" prop="address" /> -->
      <!-- <el-table-column label="描述" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDoubleClick(scope.row)"
            >查看</el-button
          >

          <el-button
            v-show="isShow == 'true'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wb:wbMaintainCompany:edit']"
            >修改</el-button
          >
          <el-button
            v-show="isShow == 'true'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wb:wbMaintainCompany:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维保公司对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :disabled="isDetails"
        :rules="rules"
        label-width="110px"
        :inline="true"
        class="dialog-form"
      >
        <el-form-item label="公司名称" prop="companyName" class="form-item-row">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>

        <el-form-item
          v-if="title == '添加维保公司'"
          label="管理员账号"
          prop="userName"
        >
          <el-input v-model="form.userName" placeholder="请输入管理员账号" />
        </el-form-item>
        <el-form-item
          v-if="title == '添加维保公司'"
          label="密码"
          prop="passWord"
        >
          <el-input
            v-model="form.passWord"
            placeholder="请输入用户密码"
            type="password"
            maxlength="20"
            show-password
          />
        </el-form-item>

        <el-form-item label="联系人/法人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人/法人" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="公司简介" prop="intro" class="form-item-row">
          <el-input
            v-model="form.intro"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="所在地址:" prop="province">
          <provice-select
            :isShowDistrict="true"
            :defaultCity="defaultCity"
            @changeValue="changeValue"
          ></provice-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address" class="form-item-row">
          <el-input v-model="form.address" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="描述" prop="remark" class="form-item-row">
          <el-input
            v-model="form.remark"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入描述"
          />
        </el-form-item>
        <el-form-item
          label="法人身份证正面"
          prop="idCardPicA"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form.idCardPicA" :limit="1" />
        </el-form-item>
        <el-form-item
          label="法人身份证反面"
          prop="idCardPicB"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form.idCardPicB" :limit="1" />
        </el-form-item>
        <el-form-item
          label="营业执照"
          prop="businessLicense"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form.businessLicense" :limit="1" />
        </el-form-item>

        <el-form-item
          label="公司logo"
          prop="logo"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form.logo" :limit="1" />
        </el-form-item>

        <el-form-item
          label="公章"
          prop="commonSeal"
          class="row"
          label-width="200px"
        >
          <image-upload v-model="form.commonSeal" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="isDetails" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listWbMaintainCompany,
  getWbMaintainCompany,
  delWbMaintainCompany,
  addWbMaintainCompany,
  updateWbMaintainCompany,
} from "@/api/wb/wbMaintainCompany";
import ProviceSelect from "@/components/ProviceSelect/index.vue";
import { fomatterAdressName } from "@/utils/index.js";
export default {
  name: "WbMaintainCompany",
  components: { ProviceSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维保公司表格数据
      wbMaintainCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        idCard: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
        ],

        passWord: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],

        contact: [
          { required: true, message: "联系人/法人不能为空", trigger: "blur" },
        ],
        province: [
          { required: true, message: "所在地不能为空", trigger: "change" },
        ],
        idCardPicA: [
          {
            required: true,
            message: "法人身份证正面不能为空",
            trigger: "blur",
          },
        ],

        idCardPicB: [
          {
            required: true,
            message: "法人身份证反面不能为空",
            trigger: "blur",
          },
        ],

        logo: [
          { required: true, message: "公司logo不能为空", trigger: "blur" },
        ],

        businessLicense: [
          { required: true, message: "营业执照不能为空", trigger: "blur" },
        ],
        commonSeal: [
          { required: true, message: "公章不能为空", trigger: "blur" },
        ],

        phoneNumber: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        idCard: [
          { required: true, message: "身份证号为空", trigger: "blur" },
          {
            pattern:
              /^[1-9]\d{5}(18|19|20|21|22)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|[Xx])$/,
            message: "请输入正确的身份证号",
            trigger: "blur",
          },
        ],

        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "用户名称长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "用户密码不能为空", trigger: "blur" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur",
          },
          {
            pattern: /^[^<>"'|\\]+$/,
            message: "不能包含非法字符：< > \" ' \\\ |",
            trigger: "blur",
          },
        ],
      },
      defaultCity: {
        province: "",
        city: "",
        district: "",
      },
      defaultCity1: {
        province: "",
        city: "",
        district: "",
      },
      isDetails: false,
      isShow: false,
    };
  },
  created() {
    this.getList();
    this.isShow = window.localStorage.getItem("isShow");
    console.log(this.isShow, "isShow");
  },
  methods: {
    fomatterAdressName,
    changeValue(values) {
      this.form.province = values[0];
      this.form.city = values[1];
      this.form.county = values[2];
    },
    queryChangeValue(values) {
      console.log(values);
      this.queryParams.province = values[0];
      this.queryParams.city = values[1];
      this.queryParams.county = values[2];
    },
    /** 查询维保公司列表 */
    getList() {
      this.loading = true;
      listWbMaintainCompany(this.queryParams).then((response) => {
        this.wbMaintainCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleDoubleClick(row) {
      this.isDetails = true;

      this.reset();
      const id = row.id || this.ids;
      getWbMaintainCompany(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "查看维保公司";
        this.defaultCity = {
          province: response.data.province ? response.data.province + "" : "",
          city: response.data.city ? response.data.city + "" : "",
          district: response.data.county ? response.data.county + "" : "",
        };
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.isDetails = false;

      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        intro: null,
        contact: null,
        idCard: null,
        idCardPicA: null,
        idCardPicB: null,
        businessLicense: null,
        phoneNumber: null,
        address: null,
        remark: null,
        createTime: null,
        createBy: null,
        userName: null,
        passWord: null,
        updateTime: null,
        updateBy: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.province = "";
      this.queryParams.city = "";
      this.queryParams.county = "";
      this.defaultCity1 = {
        province: "",
        city: "",
        district: "",
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isDetails = false;
      this.defaultCity = {
        province: "",
        city: "",
        district: "",
      };
      this.reset();
      this.open = true;
      this.title = "添加维保公司";

      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isDetails = false;

      const id = row.id || this.ids;
      getWbMaintainCompany(id).then((response) => {
        this.form = response.data;
        this.defaultCity = {
          province: response.data.province ? response.data.province + "" : "",
          city: response.data.city ? response.data.city + "" : "",
          district: response.data.county ? response.data.county + "" : "",
        };
        this.open = true;
        this.title = "修改维保公司";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWbMaintainCompany(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWbMaintainCompany(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除维保公司编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWbMaintainCompany(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wb/wbMaintainCompany/export",
        {
          ...this.queryParams,
        },
        `wbMaintainCompany_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
